﻿/************************************************************************
* Copyright (c) 2018 All Rights Reserved.
*命名空间：DAL.Base
*文件名： BaseDal
*创建人： kingling
*创建时间：2018/6/19 12:44:42
*描述
*=======================================================================
*修改标记
*修改时间：2018/6/19 12:44:42
*修改人：kingling
*描述：
************************************************************************/

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace DAL.Base
{
    public class BaseDal<T> where T : class, new()
    {
        ContextBase context { get; set; }
        DbSet<T> dbset { get; set; }
        public BaseDal(ContextBase context)
        {
            this.context = context;
            dbset = this.context.Set<T>();
        }

        public bool Add(IEnumerable<T> entities, bool isCommit = true)
        {
            dbset.AddRange(entities);
            return isCommit ? this.Save() : false;
        }

        public bool Add(T entity, bool isCommit = true)
        {
            dbset.Add(entity);
            return isCommit ? this.Save() : false;
        }

        public bool Delete(T entity, bool isCommit = true)
        {
            dbset.Remove(entity);
            return isCommit ? this.Save() : false;
        }

        public bool Delete(IEnumerable<T> entities, bool isCommit = true)
        {
            dbset.RemoveRange(entities);
            return isCommit ? this.Save() : false;
        }
        public bool Update(T entities, bool isCommit = true)
        {

            var entry = context.Entry<T>(entities);
            if (entry.State == EntityState.Detached)
            {
                context.Set<T>().Attach(entities);
                entry.State = EntityState.Modified;
            }
            return isCommit ? this.Save() : false;
        }
        public IQueryable<T> Query(Expression<Func<T, bool>> where)
        {
            return dbset.Where(where).AsQueryable();
        }
        public IQueryable<T> Query(Expression<Func<T, bool>> where, params Expression<Func<T, object>>[] includes)
        {
            var db = dbset.Where(where);
            foreach (var item in includes)
            {
                db = db.Include(item);
            }
            return db.AsQueryable();
        }
        public IQueryable<T> QueryAsNoTracking(Expression<Func<T, bool>> where, params Expression<Func<T, object>>[] includes)
        {
            var db = dbset.Where(where);
            foreach (var item in includes)
            {
                db = db.Include(item);
            }
            return db.AsNoTracking();
        }
        public IQueryable<T> Query(Expression<Func<T, bool>> where, params string[] paths)
        {
            var db = dbset.Where(where);
            foreach (var item in paths)
            {
                db = db.Include(item);
            }
            return db.AsQueryable();
        }
        public int Count(Expression<Func<T, bool>> where)
        {
            return dbset.Count(where);
        }
        public bool Save()
        {
            try
            {
                return context.SaveChanges() > 0;
            }
            catch
            {
                throw;
            }
        }
        public IQueryable<T> QueryByPage<TKey>(Expression<Func<T, bool>> where, Expression<Func<T, TKey>> orderby, out int total, out int pageSize, int page = 1, bool IsAsc = true, params Expression<Func<T, object>>[] includes)
        {
            total = dbset.Where(where).Count();
            pageSize = 10;
            var db = dbset.Where(where);
            foreach (var item in includes)
            {
                db = db.Include(item);
            }
            if (IsAsc)
            {
                return db.OrderBy(orderby).Skip((page - 1) * pageSize).Take(pageSize).AsQueryable();
            }
            else
            {
                return db.OrderByDescending(orderby).Skip((page - 1) * pageSize).Take(pageSize).AsQueryable();
            }
        }
    }
}

